
package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.BomDto;
import com.yc.sole.erp.entity.Bom;
import com.yc.sole.erp.mapper.BomMapper;
import com.yc.sole.erp.request.BomQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-24
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class BomRepository extends ServiceImpl<BomMapper, Bom> {

    /**
     * 新增
     *
     * @param bomDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBom(BomDto bomDto) {
        Bom entity = new Bom();
        BeanUtil.copyProperties(bomDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param bomDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBomById(BomDto bomDto) {
        Bom entity = new Bom();
        BeanUtil.copyProperties(bomDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBomByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<BomDto> selectBomByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), BomDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public BomDto selectBomById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), BomDto.class);
    }

    /**
     * 根据产品id获取
     *
     * @param productIds
     * @return RestResult
     */
    public List<BomDto> selectBomByProductIds(Collection<Long> productIds) {
        LambdaQueryWrapper<Bom> wrapper = Wrappers.<Bom>lambdaQuery().in(Bom::getProductId, productIds);
        return BeanUtil.copyToList(this.list(wrapper), BomDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<BomDto> selectBomPage(Paging page, BomQueryReq bomQueryReq) {
        LambdaQueryWrapper<Bom> queryWrapper = Wrappers.<Bom>lambdaQuery()
                .eq(Objects.nonNull(bomQueryReq.getProductId()), Bom::getProductId, bomQueryReq.getProductId());
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), BomDto.class);
    }
}
